import React from "react";
import type { FormProps } from "antd";
import { Button, Checkbox, Form, Input } from "antd";
import "./reg.css";
import { getRegister } from "../../request";
import { useNavigate } from "react-router-dom";

type FieldType = {
  phone?: string;
  password?: string;
  remember?: string;
};



function Index() {
const navigate=useNavigate();
const onFinish: FormProps<FieldType>["onFinish"] = (values) => {
  getRegister(values).then(res=>{
    var {data:{code,token}}=res;
    if(code===200){
      alert('注册成功')
      localStorage.setItem('token',token)
      // window.location.href='/login'
      navigate('/login')
    }
  })
  console.log("Success:", values);
};

const onFinishFailed: FormProps<FieldType>["onFinishFailed"] = (errorInfo) => {
  console.log("Failed:", errorInfo);
};
  return (
    <div className="register">
      <div className="reg">
        {/* <h3>注册</h3> */}
        <Form
          name="basic"
          labelCol={{ span: 8 }}
          wrapperCol={{ span: 16 }}
          style={{ maxWidth: 600 }}
          initialValues={{ remember: true }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item<FieldType>
            label="手机号"
            name="phone"
            rules={[
              { required: true, message: "请输入手机号!" },
              { pattern: /^[0-9]{11}$/, message: "手机号必须是11位数字" },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item<FieldType>
            label="密码"
            name="password"
            rules={[
              { required: true, message: "请输入密码!" },
              {
                pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/,
                message: "密码必须6位，包含字母和数字",
              },
            ]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
            <Button type="primary" htmlType="submit">
              注册
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}

export default Index;
